package com.penghaisoft.microwms.product.web.controller.outwarehouse;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.ProHandOut;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IWmsCommonService;
import com.penghaisoft.microwms.product.service.IProHandOutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 手工chu库
 *
 * @author liwenjiang
 */
@RestController
@RequestMapping(value = "/hand")
@Slf4j
public class ProHandOutController extends BaseController {
	@Autowired
	private IWmsCommonService wmsCommonService;

	@Autowired
	private IProHandOutService proHandOutService;

	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
							@RequestParam(name = "rows", defaultValue = "10") int rows, ProHandOut condition) {
		Pager<ProHandOut> result = proHandOutService.findListByCondition(page, rows, condition);
		return success(result);

	}

	/**
	 * 手工出库创建
	 */
	@PostMapping("insertHand")
	public TransResult insertHand(@RequestBody List<ProHandOut> list) {
		TransResult transResult = new TransResult();
		if (list == null || list.isEmpty()) {
			transResult.setCode(Constant.FAIL);
			transResult.setMsg("请选择型号以及数量");
			return transResult;
		} else {
			//创建5位调拨流水号-手工出库单号
//    		String handOutNo = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_XSC, Constant.SEQ_PREFIX_SHORT_REDIS_PRO_XSC, 1).get(0);
			String handOutNo = wmsCommonService.getProShortSeq1("", Constant.SEQ_PREFIX_OUTWH_REDIS, Constant.SEQ_PREFIX_SHORT_REDIS_PRO_XSC, 1).get(0);
			// 区别销售出库，在出库单号前加 0 识别
			Resp resp = proHandOutService.insert(list, handOutNo, getLoginName());
			transResult.setCode(resp.getCode());
			transResult.setMsg(resp.getMsg());
		}
		return transResult;
	}

	/**
	 * 手工出库修改-根据手工出库单条修改
	 */
	@PostMapping("update")
	public TransResult update(ProHandOut condition) {
		TransResult transResult = new TransResult();
		transResult.setCode(Constant.SUCCESS);
		//获取选中的行项目ID
		ProHandOut hands = new ProHandOut();
		hands.setActiveFlag("1");
		hands.setHandOutId(condition.getHandOutId());
		//查询手工表
		List<ProHandOut> list = proHandOutService.queryListByAny(hands);
		if (list != null && !list.isEmpty()) {
			ProHandOut handOut = list.get(0);//获取list的第一条数据
			if (handOut.getHandOutStatus() != null && !handOut.getHandOutStatus().equals("0")) {
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("该手工出库单号出库中或者出库完成，不能修改");
				return transResult;
			}
		}
		condition.setLastModifiedBy(getLoginName());
		condition.setGmtModified(new Date());
		proHandOutService.update(condition);

		return transResult;

	}

	@PostMapping("updateByNo")
	public TransResult updateByNo(ProHandOut condition) {
		TransResult transResult = new TransResult();
		transResult.setCode(Constant.SUCCESS);
		//获取选中的行项目ID
		ProHandOut hands = new ProHandOut();
		hands.setActiveFlag("1");
		hands.setHandOutNo(condition.getHandOutNo());
		//查询手工表
		List<ProHandOut> list = proHandOutService.queryListByAny(hands);
		if (list != null && !list.isEmpty()) {
//    		ProHandOut handOut = list.get(0);//获取list的第一条数据
			int size = list.size();
			for (int i = 0; i < size; i++) {
				if (list.get(i).getHandOutStatus() != null && !list.get(i).getHandOutStatus().equals("0")) {
					transResult.setCode(Constant.FAIL);
					transResult.setMsg("该手工出库单号出库中或者出库完成，不能修改");
					return transResult;
				}
			}
		}
		condition.setLastModifiedBy(getLoginName());
		condition.setGmtModified(new Date());
		proHandOutService.updateByNo(condition);

		return transResult;

	}

	/**
	 * 根据handOutId  查询
	 *
	 * @return
	 */
	@GetMapping("get/id/{handOutId}")
	public TransResult getHandOutId(@PathVariable("handOutId") String handOutId) {
		TransResult transResult = new TransResult();
		transResult.setCode(Constant.SUCCESS);
		ProHandOut handOut = new ProHandOut();
		handOut.setActiveFlag("1");
		handOut.setHandOutId(Integer.parseInt(handOutId));

		List<ProHandOut> list = proHandOutService.queryListByAny(handOut);
		if (list != null && !list.isEmpty()) {
			ProHandOut tmp = list.get(0);
			if (tmp.getHandOutStatus() == null) {
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("该手工出库单号 出库状态未知！");
				return transResult;
			} else if (!tmp.getHandOutStatus().equals("0")) {
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("该手工出库单号不能修改！");
				return transResult;
			}
			transResult.setData(tmp);
		}

		return transResult;

	}

	/**
	 * 审核
	 *
	 * @param handOut
	 * @return
	 */
	@PostMapping("audit")
	public TransResult audit(ProHandOut handOut) {
		TransResult result = new TransResult();
		handOut.setLastModifiedBy(getLoginName());
		handOut.setGmtModified(new Date());

		ProHandOut pro = new ProHandOut();
		pro.setActiveFlag("1");
//    	pro.setHandOutNo(handOut.getHandOutNo());
		pro.setHandOutId(handOut.getHandOutId());

		List<ProHandOut> list = proHandOutService.queryListByAny(pro);
		if (list != null && !list.isEmpty()) {
//			0新建 1出库中 2出库完成 3审核通过 4审核不通过
			if (!list.get(0).getHandOutStatus().equals("0")) {
				result.setCode(Constant.FAIL);
				result.setMsg("该类型手工出库单不能审核！");
				return result;
			}
		}
		//更新时，只改出库状态是  新建的
		handOut.setStatus("0");
		if (handOut.getHandOutStatus().equals("4")) {
			handOut.setActiveFlag(Constant.FAIL);
		}
		Resp resp = proHandOutService.updateByNo(handOut);
		result.setCode(resp.getCode());
		result.setMsg(resp.getMsg());
		return result;

	}

	@PostMapping("audit/alone")
	public TransResult auditAlone(ProHandOut handOut) {
		TransResult result = new TransResult();
		handOut.setLastModifiedBy(getLoginName());
		handOut.setGmtModified(new Date());

		ProHandOut pro = new ProHandOut();
		pro.setActiveFlag("1");
//    	pro.setHandOutNo(handOut.getHandOutNo());
		pro.setHandOutId(handOut.getHandOutId());

		List<ProHandOut> list = proHandOutService.queryListByAny(pro);
		if (list != null && !list.isEmpty()) {
//			0新建 1出库中 2出库完成 3审核通过 4审核不通过
			if (!list.get(0).getHandOutStatus().equals("0")) {
				result.setCode(Constant.FAIL);
				result.setMsg("该类型手工出库单不能审核！");
				return result;
			}
		}
		if (handOut.getHandOutStatus().equals("4")) {
			handOut.setActiveFlag(Constant.FAIL);
		}
		Resp resp = proHandOutService.update(handOut);
		result.setCode(resp.getCode());
		result.setMsg(resp.getMsg());
		return result;

	}

	@PostMapping("print")
	public TransResult print(ProHandOut handOut) {
		TransResult result = new TransResult();
		result = proHandOutService.queryPrint(handOut);
		return result;
	}


	/**
	 * @Title: toHand
	 * @Description: 手工出库转销售出库
	 * @author wzg
	 * @date 2019年4月3日
	 */
	@PostMapping("toSell")
	public TransResult toHand(ProHandOut handOut) {
		TransResult result = new TransResult();
//		result = proSellOutService.sellToHand(handOut);
		result = proHandOutService.handToSell(handOut);
		return result;
	}

}
